package ewms.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import ewms.domain.EwmsOutWarehouseDetail;
import ewms.domain.EwmsWasteInventory;
import ewms.mapper.EwmsOutWarehouseDetailMapper;
import ewms.mapper.EwmsWasteInventoryMapper;
import ewms.utils.EwmsUtils;
import ewms.common.exception.CustomException;
import ewms.common.utils.PageUtils;
import ewms.common.core.page.PagePlus;
import ewms.common.core.page.TableDataInfo;
import ewms.service.IEwmsIncinerateHandleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import ewms.bo.EwmsIncinerateHandleAddBo;
import ewms.bo.EwmsIncinerateHandleQueryBo;
import ewms.bo.EwmsIncinerateHandleEditBo;
import ewms.domain.EwmsIncinerateHandle;
import ewms.mapper.EwmsIncinerateHandleMapper;
import ewms.vo.EwmsIncinerateHandleVo;

import javax.validation.constraints.NotNull;
import java.util.List;
import java.util.Map;
import java.util.Collection;
import java.util.Objects;

/**
 * 焚化处置Service业务层处理
 *
 * @author zhaoxiaochun
 * @date 2022-02-17
 */
@Service
public class EwmsIncinerateHandleServiceImpl extends ServiceImpl<EwmsIncinerateHandleMapper, EwmsIncinerateHandle> implements IEwmsIncinerateHandleService {

	@Autowired
	private EwmsOutWarehouseDetailMapper outWarehouseDetailMapper;
	@Autowired
	private EwmsWasteInventoryMapper wasteInventoryMapper;

	@Override
	public EwmsIncinerateHandleVo queryById(Long incinerateHandleId) {
		return getVoById(incinerateHandleId, EwmsIncinerateHandleVo.class);
	}

	@Override
	public TableDataInfo<EwmsIncinerateHandleVo> selectPageIncinerateHandleList(EwmsIncinerateHandleQueryBo bo) {
		return PageUtils.buildDataInfo(baseMapper.selectPageIncinerateHandleList(PageUtils.buildPage(), bo));
	}

	@Override
	public List<EwmsIncinerateHandleVo> selectIncinerateHandleList(EwmsIncinerateHandleQueryBo bo) {
		return baseMapper.selectIncinerateHandleList(bo);
	}

	@Override
	public TableDataInfo<EwmsIncinerateHandleVo> queryPageList(EwmsIncinerateHandleQueryBo bo) {
		PagePlus<EwmsIncinerateHandle, EwmsIncinerateHandleVo> result = pageVo(PageUtils.buildPagePlus(), buildQueryWrapper(bo), EwmsIncinerateHandleVo.class);
		return PageUtils.buildDataInfo(result);
	}

	@Override
	public List<EwmsIncinerateHandleVo> queryList(EwmsIncinerateHandleQueryBo bo) {
		return listVo(buildQueryWrapper(bo), EwmsIncinerateHandleVo.class);
	}

	private LambdaQueryWrapper<EwmsIncinerateHandle> buildQueryWrapper(EwmsIncinerateHandleQueryBo bo) {
		Map<String, Object> params = bo.getParams();
		LambdaQueryWrapper<EwmsIncinerateHandle> lqw = Wrappers.lambdaQuery();
		lqw.like(StrUtil.isNotBlank(bo.getWasteName()), EwmsIncinerateHandle::getWasteName, bo.getWasteName());
		lqw.like(StrUtil.isNotBlank(bo.getBatchNumber()), EwmsIncinerateHandle::getBatchNumber, bo.getBatchNumber());
		lqw.between(params.get("beginIncinerateDate") != null && params.get("endIncinerateDate") != null,
			EwmsIncinerateHandle::getIncinerateDate, params.get("beginIncinerateDate"), params.get("endIncinerateDate"));
		lqw.like(StrUtil.isNotBlank(bo.getCreateBy()), EwmsIncinerateHandle::getCreateBy, bo.getCreateBy());
		return lqw;
	}

	@Override
	public Boolean insertByAddBo(EwmsIncinerateHandleAddBo bo) {
		boolean saveFlag = true;
		EwmsIncinerateHandle add = BeanUtil.toBean(bo, EwmsIncinerateHandle.class);
		add.setIncinerateDate(EwmsUtils.handleSelectDate(add.getIncinerateDate()));
		@NotNull(message = "出库明细ID列表不能为空") List<Long> outWarehouseDetailIdList = bo.getOutWarehouseDetailIdList();
		for (Long outWarehouseDetailId : outWarehouseDetailIdList) {
			EwmsOutWarehouseDetail ewmsOutWarehouseDetail = outWarehouseDetailMapper.selectById(outWarehouseDetailId);
			if (Objects.isNull(ewmsOutWarehouseDetail)) {
				throw new CustomException("危废出库明细信息未找到");
			}

			EwmsWasteInventory ewmsWasteInventory = wasteInventoryMapper.selectById(ewmsOutWarehouseDetail.getWasteInventoryId());
			if (Objects.isNull(ewmsWasteInventory)) {
				throw new CustomException("危废库存信息未找到");
			}

			add.setWasteId(ewmsWasteInventory.getWasteId())
				.setWasteName(ewmsWasteInventory.getWasteName())
				.setBatchNumber(ewmsWasteInventory.getBatchNumber())
				.setOutWarehouseDetailId(ewmsOutWarehouseDetail.getOutWarehouseDetailId());

			validEntityBeforeSave(add);
			saveFlag = saveFlag && SqlHelper.retBool(baseMapper.insert(add));
		}
		return saveFlag;
	}

	@Override
	public Boolean updateByEditBo(EwmsIncinerateHandleEditBo bo) {
		EwmsIncinerateHandle update = BeanUtil.toBean(bo, EwmsIncinerateHandle.class);
		validEntityBeforeSave(update);
		return updateById(update);
	}

	/**
	 * 保存前的数据校验
	 *
	 * @param entity 实体类数据
	 */
	private void validEntityBeforeSave(EwmsIncinerateHandle entity) {
		//TODO 做一些数据校验,如唯一约束
	}

	@Override
	public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
		if (isValid) {
			//TODO 做一些业务上的校验,判断是否需要校验
		}
		return SqlHelper.retBool(baseMapper.deleteBatchIds(ids));
	}
}
